/*
 * Copyright (c) 2022 OpenLuat & AirM2M
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "user.h"

/* OV2640 Registers definition when DSP bank selected (0xFF = 0x00) */
//#define OV2640_DSP_R_BYPASS   },05
#define OV2640_DSP_Qs           0x44
#define OV2640_DSP_CTRL         0x50
#define OV2640_DSP_HSIZE1       0x51
#define OV2640_DSP_VSIZE1       0x52
#define OV2640_DSP_XOFFL        0x53
#define OV2640_DSP_YOFFL        0x54
#define OV2640_DSP_VHYX         0x55
#define OV2640_DSP_DPRP         0x56
#define OV2640_DSP_TEST         0x57
#define OV2640_DSP_ZMOW         0x5A
#define OV2640_DSP_ZMOH         0x5B
#define OV2640_DSP_ZMHH         0x5C
#define OV2640_DSP_BPADDR       0x7C
#define OV2640_DSP_BPDATA       0x7D
#define OV2640_DSP_CTRL2        0x86
#define OV2640_DSP_CTRL3        0x87
#define OV2640_DSP_SIZEL        0x8C
#define OV2640_DSP_HSIZE2       0xC0
#define OV2640_DSP_VSIZE2       0xC1
#define OV2640_DSP_CTRL0        0xC2
#define OV2640_DSP_CTRL1        0xC3
#define OV2640_DSP_R_DVP_SP     0xD3
#define OV2640_DSP_IMAGE_MODE   0xDA
#define OV2640_DSP_RESET        0xE0
#define OV2640_DSP_MS_SP        0xF0
#define OV2640_DSP_SS_ID        0x7F
#define OV2640_DSP_SS_CTRL      0xF8
#define OV2640_DSP_MC_BIST      0xF9
#define OV2640_DSP_MC_AL        0xFA
#define OV2640_DSP_MC_AH        0xFB
#define OV2640_DSP_MC_D         0xFC
#define OV2640_DSP_P_STATUS     0xFE
#define OV2640_DSP_RA_DLMT      0xFF

/* OV2640 Registers definition when sensor bank selected (0xFF = 0x01) */
#define OV2640_SENSOR_GAIN       0x00
#define OV2640_SENSOR_COM1       0x03
#define OV2640_SENSOR_REG04      0x04
#define OV2640_SENSOR_REG08      0x08
#define OV2640_SENSOR_COM2       0x09
#define OV2640_SENSOR_PIDH       0x0A
#define OV2640_SENSOR_PIDL       0x0B
#define OV2640_SENSOR_COM3       0x0C
#define OV2640_SENSOR_COM4       0x0D
#define OV2640_SENSOR_AEC        0x10
#define OV2640_SENSOR_CLKRC      0x11
#define OV2640_SENSOR_COM7       0x12
#define OV2640_SENSOR_COM8       0x13
#define OV2640_SENSOR_COM9       0x14
#define OV2640_SENSOR_COM10      0x15
#define OV2640_SENSOR_HREFST     0x17
#define OV2640_SENSOR_HREFEND    0x18
#define OV2640_SENSOR_VSTART     0x19
#define OV2640_SENSOR_VEND       0x1A
#define OV2640_SENSOR_MIDH       0x1C
#define OV2640_SENSOR_MIDL       0x1D
#define OV2640_SENSOR_AEW        0x24
#define OV2640_SENSOR_AEB        0x25
#define OV2640_SENSOR_W          0x26
#define OV2640_SENSOR_REG2A      0x2A
#define OV2640_SENSOR_FRARL      0x2B
#define OV2640_SENSOR_ADDVSL     0x2D
#define OV2640_SENSOR_ADDVHS     0x2E
#define OV2640_SENSOR_YAVG       0x2F
#define OV2640_SENSOR_REG32      0x32
#define OV2640_SENSOR_ARCOM2     0x34
#define OV2640_SENSOR_REG45      0x45
#define OV2640_SENSOR_FLL        0x46
#define OV2640_SENSOR_FLH        0x47
#define OV2640_SENSOR_COM19      0x48
#define OV2640_SENSOR_ZOOMS      0x49
#define OV2640_SENSOR_COM22      0x4B
#define OV2640_SENSOR_COM25      0x4E
#define OV2640_SENSOR_BD50       0x4F
#define OV2640_SENSOR_BD60       0x50
#define OV2640_SENSOR_REG5D      0x5D
#define OV2640_SENSOR_REG5E      0x5E
#define OV2640_SENSOR_REG5F      0x5F
#define OV2640_SENSOR_REG60      0x60
#define OV2640_SENSOR_HISTO_LOW  0x61
#define OV2640_SENSOR_HISTO_HIGH 0x62

#define GC032A_I2C_ADDRESS 0x21
#define OV2640_I2C_ADDRESS 0x30
static const I2C_CommonRegDataStruct GC032A_InitRegQueue[] =
{
        {0xf3, 0xff},
        {0xf7, 0x01},
		{0xf8, 0x03},
        {0xf9, 0xce},
        {0xfa, 0x00},
        {0xfc, 0x02},
        {0xfe, 0x02},
        {0x81, 0x03},


		{0xfe,0x00},
		{0x03,0x01},
		{0x04,0xc2},
		{0x05,0x01},
		{0x06,0xc2},
		{0x07,0x00},
		{0x08,0x08},
		{0x0a,0x04},
		{0x0c,0x04},
		{0x0d,0x01},
		{0x0e,0xe8},
		{0x0f,0x02},
		{0x10,0x88},
		//{0x17,0x55},
		{0x17,0x54},
		{0x19,0x04},
		{0x1a,0x0a},
		{0x1f,0x40},
		{0x20,0x30},
		{0x2e,0x80},
		{0x2f,0x2b},
		{0x30,0x1a},
		{0xfe,0x02},
		{0x03,0x02},
		{0x06,0x60},
		{0x05,0xd7},
		{0x12,0x89},
		{0xfe,0x00},
		{0x18,0x02},
		{0xfe,0x02},
		{0x40,0x22},
		{0x45,0x00},
		{0x46,0x00},
		{0x49,0x20},
		{0xfe,0x01},
		{0x0a,0xc5},
		{0x45,0x00},
		{0xfe,0x00},
		{0x40,0xff},
		{0x41,0x25},
		{0x42,0x83},
		{0x43,0x10},
//		{0x44,0x90},
		{0x46,0x26},
		{0x49,0x03},
		{0x4f,0x01},
		{0xde,0x84},
		{0xfe,0x02},
		{0x22,0xf6},
		{0xfe,0x01},
		{0x12,0xa0},
		{0x13,0x3a},
		{0xc1,0x3c},
		{0xc2,0x50},
		{0xc3,0x00},
		{0xc4,0x32},
		{0xc5,0x24},
		{0xc6,0x16},
		{0xc7,0x08},
		{0xc8,0x08},
		{0xc9,0x00},
		{0xca,0x20},
		{0xdc,0x8a},
		{0xdd,0xa0},
		{0xde,0xa6},
		{0xdf,0x75},
		{0xfe,0x01},
		{0x7c,0x09},
		{0x65,0x00},
		{0x7c,0x08},
		{0x56,0xf4},
		{0x66,0x0f},
		{0x67,0x84},
		{0x6b,0x80},
		{0x6d,0x12},
		{0x6e,0xb0},
		{0x86,0x00},
		{0x87,0x00},
		{0x88,0x00},
		{0x89,0x00},
		{0x8a,0x00},
		{0x8b,0x00},
		{0x8c,0x00},
		{0x8d,0x00},
		{0x8e,0x00},
		{0x8f,0x00},
		{0x90,0xef},
		{0x91,0xe1},
		{0x92,0x0c},
		{0x93,0xef},
		{0x94,0x65},
		{0x95,0x1f},
		{0x96,0x0c},
		{0x97,0x2d},
		{0x98,0x20},
		{0x99,0xaa},
		{0x9a,0x3f},
		{0x9b,0x2c},
		{0x9c,0x5f},
		{0x9d,0x3e},
		{0x9e,0xaa},
		{0x9f,0x67},
		{0xa0,0x60},
		{0xa1,0x00},
		{0xa2,0x00},
		{0xa3,0x0a},
		{0xa4,0xb6},
		{0xa5,0xac},
		{0xa6,0xc1},
		{0xa7,0xac},
		{0xa8,0x55},
		{0xa9,0xc3},
		{0xaa,0xa4},
		{0xab,0xba},
		{0xac,0xa8},
		{0xad,0x55},
		{0xae,0xc8},
		{0xaf,0xb9},
		{0xb0,0xd4},
		{0xb1,0xc3},
		{0xb2,0x55},
		{0xb3,0xd8},
		{0xb4,0xce},
		{0xb5,0x00},
		{0xb6,0x00},
		{0xb7,0x05},
		{0xb8,0xd6},
		{0xb9,0x8c},
		{0xfe,0x01},
		{0xd0,0x40},
		{0xd1,0xf8},
		{0xd2,0x00},
		{0xd3,0xfa},
		{0xd4,0x45},
		{0xd5,0x02},
		{0xd6,0x30},
		{0xd7,0xfa},
		{0xd8,0x08},
		{0xd9,0x08},
		{0xda,0x58},
		{0xdb,0x02},
		{0xfe,0x00},
		{0xfe,0x00},
		{0xba,0x00},
		{0xbb,0x04},
		{0xbc,0x0a},
		{0xbd,0x0e},
		{0xbe,0x22},
		{0xbf,0x30},
		{0xc0,0x3d},
		{0xc1,0x4a},
		{0xc2,0x5d},
		{0xc3,0x6b},
		{0xc4,0x7a},
		{0xc5,0x85},
		{0xc6,0x90},
		{0xc7,0xa5},
		{0xc8,0xb5},
		{0xc9,0xc2},
		{0xca,0xcc},
		{0xcb,0xd5},
		{0xcc,0xde},
		{0xcd,0xea},
		{0xce,0xf5},
		{0xcf,0xff},
		{0xfe,0x00},
		{0x5a,0x08},
		{0x5b,0x0f},
		{0x5c,0x15},
		{0x5d,0x1c},
		{0x5e,0x28},
		{0x5f,0x36},
		{0x60,0x45},
		{0x61,0x51},
		{0x62,0x6a},
		{0x63,0x7d},
		{0x64,0x8d},
		{0x65,0x98},
		{0x66,0xa2},
		{0x67,0xb5},
		{0x68,0xc3},
		{0x69,0xcd},
		{0x6a,0xd4},
		{0x6b,0xdc},
		{0x6c,0xe3},
		{0x6d,0xf0},
		{0x6e,0xf9},
		{0x6f,0xff},
		{0xfe,0x00},
		{0x70,0x50},
		{0xfe,0x00},
		{0x4f,0x01},
		{0xfe,0x01},
		{0x44,0x04},
		{0x1f,0x30},
		{0x20,0x40},
		{0x26,0x9a},
		{0x27,0x02},
		{0x28,0x0d},
		{0x29,0x02},
		{0x2a,0x0d},
		{0x2b,0x02},
		{0x2c,0x58},
		{0x2d,0x07},
		{0x2e,0xd2},
		{0x2f,0x0b},
		{0x30,0x6e},
		{0x31,0x0e},
		{0x32,0x70},
		{0x33,0x12},
		{0x34,0x0c},
		{0x3c,0x20},
		{0x3e,0x20},
		{0x3f,0x2d},
		{0x40,0x40},
		{0x41,0x5b},
		{0x42,0x82},
		{0x43,0xb7},
		{0x04,0x0a},
		{0x02,0x79},
		{0x03,0xc0},
		{0xcc,0x08},
		{0xcd,0x08},
		{0xce,0xa4},
		{0xcf,0xec},
		{0xfe,0x00},
		{0x81,0xb8},
		{0x82,0x12},
		{0x83,0x0a},
		{0x84,0x01},
		{0x86,0x25},
		{0x87,0x18},
		{0x88,0x10},
		{0x89,0x70},
		{0x8a,0x20},
		{0x8b,0x10},
		{0x8c,0x08},
		{0x8d,0x0a},
		{0xfe,0x00},
		{0x8f,0xaa},
		{0x90,0x9c},
		{0x91,0x52},
		{0x92,0x03},
		{0x93,0x03},
		{0x94,0x08},
		{0x95,0x44},
		{0x97,0x00},
		{0x98,0x00},
		{0xfe,0x00},
		{0xa1,0x30},
		{0xa2,0x41},
		{0xa4,0x30},
		{0xa5,0x20},
		{0xaa,0x30},
		{0xac,0x32},
		{0xfe,0x00},
		{0xd1,0x3c},
		{0xd2,0x3c},
		{0xd3,0x38},
		{0xd6,0xf4},
		{0xd7,0x1d},
		{0xdd,0x73},
		{0xde,0x84},
		{0xfe,0x01},
		{0x13,0x20},
		{0xfe,0x00},
		{0x4f,0x00},
		{0x03,0x00},
		{0x04,0xa0},
		{0x71,0x60},
		{0x72,0x40},
		{0x42,0x80},
		{0x77,0x64},
		{0x78,0x40},
		{0x79,0x60},

		{0xfe, 0x00},
		{0x44, 0x06},	//改成RGB565输出
        {0x46, 0x0f},	//决定了VSYNC，HSYNC, pixclk
};
//800*480
const const I2C_CommonRegDataStruct OV2640_InitRegQueue[]=
{
        {0xff, 0x00},
        {0x2c, 0xff},
        {0x2e, 0xdf},

        {0xff, 0x01},
        {0x3c, 0x32},
        {0x11, 0x00},
        {0x09, 0x02},
        {0x04, 0x20|0x80},	//水平翻转
        {0x13, 0xe5},
        {0x14, 0x48},
        {0x2c, 0x0c},
        {0x33, 0x78},
        {0x3a, 0x33},
        {0x3b, 0xfB},
        {0x3e, 0x00},
        {0x43, 0x11},
        {0x16, 0x10},
        {0x4a, 0x81},
        {0x21, 0x99},
        {0x24, 0x40},
        {0x25, 0x38},
        {0x26, 0x82},
        {0x5c, 0x00},
        {0x63, 0x00},
        {0x46, 0x3f},
        {0x0c, 0x3c},
        {0x61, 0x70},
        {0x62, 0x80},
        {0x7c, 0x05},
        {0x20, 0x80},
        {0x28, 0x30},
        {0x6c, 0x00},
        {0x6d, 0x80},
        {0x6e, 0x00},
        {0x70, 0x02},
        {0x71, 0x94},
        {0x73, 0xc1},
        {0x3d, 0x34},
        {0x5a, 0x57},
        {0x12, 0x00},
        {0x11, 0x00},
        {0x17, 0x11},
        {0x18, 0x75},
        {0x19, 0x01},
        {0x1a, 0x97},
        {0x32, 0x36},
        {0x03, 0x0f},
        {0x37, 0x40},
        {0x4f, 0xbb},
        {0x50, 0x9c},
        {0x5a, 0x57},
        {0x6d, 0x80},
        {0x6d, 0x38},
        {0x39, 0x02},
        {0x35, 0x88},
        {0x22, 0x0a},
        {0x37, 0x40},
        {0x23, 0x00},
        {0x34, 0xa0},
        {0x36, 0x1a},
        {0x06, 0x02},
        {0x07, 0xc0},
        {0x0d, 0xb7},
        {0x0e, 0x01},
        {0x4c, 0x00},

        {0xff, 0x00},
        {0xe5, 0x7f},
        {0xf9, 0xc0},
        {0x41, 0x24},
        {0xe0, 0x14},
        {0x76, 0xff},
        {0x33, 0xa0},
        {0x42, 0x20},
        {0x43, 0x18},
        {0x4c, 0x00},
        {0x87, 0xd0},
        {0x88, 0x3f},
        {0xd7, 0x03},
        {0xd9, 0x10},
        {0xd3, 0x82},
        {0xc8, 0x08},
        {0xc9, 0x80},
        {0x7d, 0x00},
        {0x7c, 0x03},
        {0x7d, 0x48},
        {0x7c, 0x08},
        {0x7d, 0x20},
        {0x7d, 0x10},
        {0x7d, 0x0e},
        {0x90, 0x00},
        {0x91, 0x0e},
        {0x91, 0x1a},
        {0x91, 0x31},
        {0x91, 0x5a},
        {0x91, 0x69},
        {0x91, 0x75},
        {0x91, 0x7e},
        {0x91, 0x88},
        {0x91, 0x8f},
        {0x91, 0x96},
        {0x91, 0xa3},
        {0x91, 0xaf},
        {0x91, 0xc4},
        {0x91, 0xd7},
        {0x91, 0xe8},
        {0x91, 0x20},
        {0x92, 0x00},
        {0x93, 0x06},
        {0x93, 0xe3},
        {0x93, 0x02},
        {0x93, 0x02},
        {0x93, 0x00},
        {0x93, 0x04},
        {0x93, 0x00},
        {0x93, 0x03},
        {0x93, 0x00},
        {0x93, 0x00},
        {0x93, 0x00},
        {0x93, 0x00},
        {0x93, 0x00},
        {0x93, 0x00},
        {0x93, 0x00},
        {0x96, 0x00},
        {0x97, 0x08},
        {0x97, 0x19},
        {0x97, 0x02},
        {0x97, 0x0c},
        {0x97, 0x24},
        {0x97, 0x30},
        {0x97, 0x28},
        {0x97, 0x26},
        {0x97, 0x02},
        {0x97, 0x98},
        {0x97, 0x80},
        {0x97, 0x00},
        {0x97, 0x00},
        {0xc3, 0xef},

        {0xff, 0x00},
        {0xba, 0xdc},
        {0xbb, 0x08},
        {0xb6, 0x24},
        {0xb8, 0x33},
        {0xb7, 0x20},
        {0xb9, 0x30},
        {0xb3, 0xb4},
        {0xb4, 0xca},
        {0xb5, 0x43},
        {0xb0, 0x5c},
        {0xb1, 0x4f},
        {0xb2, 0x06},
        {0xc7, 0x00},
        {0xc6, 0x51},
        {0xc5, 0x11},
        {0xc4, 0x9c},
        {0xbf, 0x00},
        {0xbc, 0x64},
        {0xa6, 0x00},
        {0xa7, 0x1e},
        {0xa7, 0x6b},
        {0xa7, 0x47},
        {0xa7, 0x33},
        {0xa7, 0x00},
        {0xa7, 0x23},
        {0xa7, 0x2e},
        {0xa7, 0x85},
        {0xa7, 0x42},
        {0xa7, 0x33},
        {0xa7, 0x00},
        {0xa7, 0x23},
        {0xa7, 0x1b},
        {0xa7, 0x74},
        {0xa7, 0x42},
        {0xa7, 0x33},
        {0xa7, 0x00},
        {0xa7, 0x23},
        {0xc0, 0xc8},
        {0xc1, 0x96},
        {0x8c, 0x00},
        {0x86, 0x3d},
        {0x50, 0x92},
        {0x51, 0x90},
        {0x52, 0x2c},
        {0x53, 0x00},
        {0x54, 0x00},
        {0x55, 0x88},
        {0x5a, 0x50},
        {0x5b, 0x3c},
        {0x5c, 0x00},
        {0xd3, 0x04},
        {0x7f, 0x00},
        {0xda, 0x00},
        {0xe5, 0x1f},
        {0xe1, 0x67},
        {0xe0, 0x00},
        {0xdd, 0x7f},
        {0x05, 0x00},

        {0xff, 0x00},
        {0xe0, 0x04},
        {0xc0, 0xc8},
        {0xc1, 0x96},
        {0x86, 0x3d},
        {0x50, 0x92},
        {0x51, 0x90},
        {0x52, 0x2c},
        {0x53, 0x00},
        {0x54, 0x00},
        {0x55, 0x88},
        {0x57, 0x00},
        {0x5a, 0x50},
        {0x5b, 0x3c},
        {0x5c, 0x00},
        {0xd3, 0x04},//0x04,
        {0xe0, 0x00},

        {0xFF, 0x00},
        {0x05, 0x00},
        {0xDA, 0x08},
        {0xda, 0x09},
        {0x98, 0x00},
        {0x99, 0x00},
        {0x00, 0x00},



//        {0xff, 0x00},
//        {0xe0, 0x04},
//        {0xc0, 0xc8},
//        {0xc1, 0x96},
//        {0x86, 0x35},
//        {0x50, 0x80},//必须配置这个寄存器控制时钟
//        {0x51, 0x90},
//        {0x52, 0x2c},
//        {0x53, 0x00},
//        {0x54, 0x00},
//        {0x55, 0x88},
//        {0x57, 0x00},
//        {0x5a, 0x78},//480
//        {0x5b, 0x44},//272
//        {0x5c, 0x00},
//        {0xd3, 0x04},
//        {0xe0, 0x00},
};


static uint32_t prvVLen;
static int32_t GC032A_InitDone(void *pData, void *pParam)
{
	DBG("!");
}

static int32_t prvCamera_DCMICB(void *pData, void *pParam)
{
	Buffer_Struct *RxBuf = (Buffer_Struct *)pData;
	if (!pData)
	{
//		DBG("%u", prvVLen);
		prvVLen = 0;
		return 0;
	}
	LCD_DrawStruct *draw = zalloc(sizeof(LCD_DrawStruct));
	if (!draw)
	{
		DBG("lcd flush no memory");
		return -1;
	}

	draw->Mode = SPI_MODE_0;
	draw->Speed = 48000000;
	draw->SpiID = HSPI_ID0;
	draw->CSPin = GPIOC_14;
	draw->DCPin = 72;
	draw->x1 = 0;
	draw->x2 = 239;
	draw->y1 = prvVLen;
	draw->y2 = prvVLen + 15;
	draw->xoffset = 0;
	draw->yoffset = 0;
	draw->Size = (draw->x2 - draw->x1 + 1) * (draw->y2 - draw->y1 + 1) * 2;
	draw->ColorMode = COLOR_MODE_RGB_565;
	draw->Data = OS_Malloc(draw->Size);
	if (!draw->Data)
	{
		DBG("lcd flush data no memory");
		OS_Free(draw);
		return -1;
	}
//	DBG_HexPrintf(RxBuf->Data, 8);
	memcpy(draw->Data, RxBuf->Data, draw->Size);
	Core_LCDDraw(draw);
	prvVLen += 16;
	return 0;
}

void GC032A_TestInit(void)
{
	uint8_t Reg = 0xf0;
	uint8_t Data[2];
	uint32_t i;
	GPIO_Config(GPIOD_06, 0, 0);
	GPIO_Config(GPIOD_07, 0, 0);
//	GPIO_ExtiSetHWTimerCB(GC032A_IOCB, NULL);
	GPIO_Iomux(GPIOE_01, 3);
	GPIO_Iomux(GPIOE_02, 3);
	GPIO_Iomux(GPIOE_03, 3);

	GPIO_Iomux(GPIOD_01, 3);
	GPIO_Iomux(GPIOD_02, 3);
	GPIO_Iomux(GPIOD_03, 3);
	GPIO_Iomux(GPIOD_08, 3);
	GPIO_Iomux(GPIOD_09, 3);
	GPIO_Iomux(GPIOD_10, 3);
	GPIO_Iomux(GPIOD_11, 3);
	GPIO_Iomux(GPIOE_00, 3);

	GPIO_Iomux(GPIOE_06, 2);
	GPIO_Iomux(GPIOE_07, 2);

	GPIO_Iomux(GPIOA_05, 2);
//	GPIO_Config(GPIOE_03, 1, 1);
//	GPIO_ExtiConfig(GPIOE_03, 0, 1, 0);
	HWTimer_SetPWM(HW_TIMER5, 12000000, 0, 0);
	Task_DelayMS(1);
	GPIO_Output(GPIOD_06, 1);
	GPIO_Output(GPIOD_07, 1);
	Task_DelayUS(10);
	GPIO_Output(GPIOD_07, 0);
	I2C_MasterSetup(I2C_ID0, 400000);
	Data[0] = 0xf4;
	Data[1] = 0x10;

	I2C_BlockWrite(I2C_ID0, GC032A_I2C_ADDRESS, Data, 2, 10, NULL, 0);
	I2C_BlockRead(I2C_ID0, GC032A_I2C_ADDRESS, &Reg, Data, 2, 10, NULL, 0);
	if ((0x23 == Data[0]) && (0x2a == Data[1]))
	{
		DBG("识别到GC032A控制芯片");
	}
	else
	{
		DBG("没有识别到GC032A控制芯片");
		return;
	}
	Data[0] = 0xfe;
	Data[1] = 0x00;
	Reg = 0x44;
	I2C_BlockWrite(I2C_ID0, GC032A_I2C_ADDRESS, Data, 2, 10, NULL, 0);
	I2C_BlockRead(I2C_ID0, GC032A_I2C_ADDRESS, &Reg, Data, 2, 10, NULL, 0);
	DBG("%x,%x", Data[0], Data[1]);
	for(i = 0; i < sizeof(GC032A_InitRegQueue)/sizeof(I2C_CommonRegDataStruct); i++)
	{
		I2C_BlockWrite(I2C_ID0, GC032A_I2C_ADDRESS, GC032A_InitRegQueue[i].Data, 2, 10, NULL, 0);
	}
	Task_DelayMS(1000);
//	DBG("GC032A Init Done");
//	I2C_Prepare(I2C_ID0, GC032A_I2C_ADDRESS, 1, GC032A_InitDone, 0);
//	I2C_MasterWriteRegQueue(I2C_ID0, GC032A_InitRegQueue, sizeof(GC032A_InitRegQueue)/sizeof(I2C_CommonRegDataStruct), 10, 0);
	DCMI_Setup(0, 0, 0, 8, 0);
	DCMI_SetCallback(prvCamera_DCMICB, 0);
	DCMI_SetCROPConfig(1, (480-320)/2, ((640-240)/2)*2, 320 - 1, 2*240 - 1);	//裁剪中间的240*320图像
	DCMI_CaptureSwitch(1, 1920, 0, 0, 0, NULL);	//240 * 2 / 4 * 16，一次DMA传输有16行数据，一共传输20次
	prvVLen = 0;
}

void OV2640_TestInit(void)
{
	uint8_t Reg = 0x1C;
	uint8_t Data[2];
	uint32_t i;
	GPIO_Config(GPIOD_06, 0, 0);
	GPIO_Config(GPIOD_07, 0, 0);
//	GPIO_ExtiSetHWTimerCB(GC032A_IOCB, NULL);
	GPIO_Iomux(GPIOE_01, 3);
	GPIO_Iomux(GPIOE_02, 3);
	GPIO_Iomux(GPIOE_03, 3);

	GPIO_Iomux(GPIOD_01, 3);
	GPIO_Iomux(GPIOD_02, 3);
	GPIO_Iomux(GPIOD_03, 3);
	GPIO_Iomux(GPIOD_08, 3);
	GPIO_Iomux(GPIOD_09, 3);
	GPIO_Iomux(GPIOD_10, 3);
	GPIO_Iomux(GPIOD_11, 3);
	GPIO_Iomux(GPIOE_00, 3);

	GPIO_Iomux(GPIOE_06, 2);
	GPIO_Iomux(GPIOE_07, 2);

	GPIO_Iomux(GPIOA_05, 2);
//	GPIO_Config(GPIOE_03, 1, 1);
//	GPIO_ExtiConfig(GPIOE_03, 0, 1, 0);
	HWTimer_SetPWM(HW_TIMER5, 24000000, 0, 0);
	Task_DelayMS(1);
	GPIO_Output(GPIOD_06, 1);
	GPIO_Output(GPIOD_07, 1);
	Task_DelayUS(10);
	GPIO_Output(GPIOD_07, 0);
	I2C_MasterSetup(I2C_ID0, 400000);
	Data[0] = 0xFF;
	Data[1] = 0x01;

	I2C_BlockWrite(I2C_ID0, OV2640_I2C_ADDRESS, Data, 2, 10, NULL, 0);
	I2C_BlockRead(I2C_ID0, OV2640_I2C_ADDRESS, &Reg, Data, 2, 10, NULL, 0);
	if ((0x7F == Data[0]) && (0xA2 == Data[1]))
	{
		DBG("识别到OV2640控制芯片");
	}
	else
	{
		DBG("没有识别到OV2640控制芯片");
		return;
	}
//	Data[0] = 0xfe;
//	Data[1] = 0x00;
//	Reg = 0x44;
//	I2C_BlockWrite(I2C_ID0, GC032A_I2C_ADDRESS, Data, 2, 10, NULL, 0);
//	I2C_BlockRead(I2C_ID0, GC032A_I2C_ADDRESS, &Reg, Data, 2, 10, NULL, 0);
//	DBG("%x,%x", Data[0], Data[1]);
	for(i = 0; i < sizeof(OV2640_InitRegQueue)/sizeof(I2C_CommonRegDataStruct); i++)
	{
		I2C_BlockWrite(I2C_ID0, OV2640_I2C_ADDRESS, OV2640_InitRegQueue[i].Data, 2, 10, NULL, 0);
	}
	Task_DelayMS(2000);
//	DBG("GC032A Init Done");
//	I2C_Prepare(I2C_ID0, GC032A_I2C_ADDRESS, 1, GC032A_InitDone, 0);
//	I2C_MasterWriteRegQueue(I2C_ID0, GC032A_InitRegQueue, sizeof(GC032A_InitRegQueue)/sizeof(I2C_CommonRegDataStruct), 10, 0);
	DCMI_Setup(0, 0, 1, 8, 1);
	DCMI_SetCallback(prvCamera_DCMICB, 0);
	DCMI_SetCROPConfig(1, (800-320)/2, ((600-240)/2)*2, 320 - 1, 2*240 - 1);	//裁剪中间的240*320图像
	DCMI_CaptureSwitch(1, 1920, 0, 0, 0, NULL);	//240 * 2 / 4 * 16，一次DMA传输有16行数据，一共传输20次
	prvVLen = 0;
}
